package core.tool.json.jackson;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import core.tool.common.StringHelper;
import core.tool.json.JsonHelper;
import core.tool.log.log4j.EasyLogger;

public class JacksonHelpser implements JsonHelper {

	
    private static ObjectMapper objectMapper = null;
	
    
    public JacksonHelpser(){
    	if(objectMapper==null) {
            objectMapper = new ObjectMapper();
    	}
    }
    
	
	public Object getJavaObject(String jsonStr, Class cla) {
		if(StringHelper.isEmpty(jsonStr)) return null;
		Object object=null;
		try {
			object = objectMapper.readValue(jsonStr, cla);
		} catch (Exception e) {
			e.printStackTrace();
			EasyLogger.error(jsonStr+"转换类型"+cla.getName()+"发生错误");
		}
		return object;
	}

	public String getJsonStr(Object object) {
		String josnStr=null;
		try {
			josnStr=objectMapper.writeValueAsString(object);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return josnStr;
	}

}
